package com.htfg.cardActivate.order.service.impl;

import com.htfg.cardActivate.core.ResultCode;
import com.htfg.cardActivate.core.ResultData;
import com.htfg.cardActivate.order.dao.OrderMapper;
import com.htfg.cardActivate.order.entity.Order;
import com.htfg.cardActivate.order.service.OrderService;
import com.htfg.cardActivate.user.dao.BankPersonMapper;
import com.htfg.cardActivate.user.dao.UserMapper;
import com.htfg.cardActivate.user.entity.BankPerson;
import com.htfg.cardActivate.user.entity.User;
import com.htfg.cardActivate.util.DateUtil;
import com.htfg.cardActivate.util.StringUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: 王晨缘
 * @Date: 2018/10/25 0025 16:06
 * @Description:
 */
@Service
@Transactional
public class OrderServiceImpl implements OrderService {

    private final Logger logger = Logger.getLogger(OrderServiceImpl.class);
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BankPersonMapper bankPersonMapper;

    /**
     * 创建订单
     *
     * @param uid
     * @param bankName
     * @param name
     * @param mobile
     * @param address
     * @param lon
     * @param lat
     * @param visitTime
     * @return
     */
    @Override
    public ResultData createOrder(String uid, String bankName, String name, String mobile, String address, String lon, String lat, String visitTime) {
        User user = userMapper.selectByPrimaryKey(uid);
        if (user == null) {
            return ResultData.failDate(ResultCode.USER_NOT_EXIST.getCode(), ResultCode.USER_NOT_EXIST.getMsg(), "");
        }
        int i = orderMapper.selectUidOrderCountByStatus(uid, 0);
        int j;
        if (i >= 5) {
            return ResultData.failDate(ResultCode.ORDER_COUNT_MANEY.getCode(), ResultCode.ORDER_COUNT_MANEY.getMsg(), "");
        } else {
            Order order = new Order();
            order.setUid(uid);
            order.setBankName(bankName);
            order.setName(name);
            order.setMobile(mobile);
            order.setAddress(address);
            order.setLat(lat);
            order.setLon(lon);
            if (StringUtil.isEmptys(visitTime)) {
                String format = DateUtil.getDateFormat2().format(new Date());
                visitTime = format + " 10:00:10," + format + " 06:00:00";
            }
            order.setVisitTime(visitTime);
            order.setCreateTime(DateUtil.getNewDateYmdHms());
            order.setStatus(0);
            j = orderMapper.insertSelective(order);
            if (j >= 1) {
                logger.info("创建订单成功 : " + order);
                return ResultData.successDate(j);
            }
        }
        return ResultData.failDate(j);
    }

    /**
     * 订单列表
     *
     * @param uid
     * @param status
     * @return
     */
    @Override
    public ResultData userOrderList(String uid, Integer status, Integer pageIndex, Integer pageSize) {
        List<Map<String, Object>> maps = orderMapper.selectOrderList(uid, status, (pageIndex - 1) * pageSize, pageSize);
        int i = orderMapper.selectOrderListCount(uid, status);
        Map<String, Object> map = new HashMap<>();
        map.put("list", maps);
        map.put("listCount", i);
        logger.info("展示订单 : " + map);
        return ResultData.successDate(map);
    }

    @Override
    public ResultData bankPersonOrderList(String uid, Integer status, Integer pageIndex, Integer pageSize) {
        List<Map<String, Object>> maps = orderMapper.bankPersonOrderList(uid, status, (pageIndex - 1) * pageSize, pageSize);
        int i = orderMapper.bankPersonOrderListCount(uid, status);
        Map<String, Object> map = new HashMap<>();
        map.put("list", maps);
        map.put("listCount", i);
        logger.info("展示订单 : " + map);
        return ResultData.successDate(map);
    }

    /**
     * 订单Id
     *
     * @param id
     * @return
     */
    @Override
    public ResultData orderDetail(Integer id) {
        Order order = orderMapper.selectByPrimaryKey(id);
        Map<String, Object> map = new HashMap<>();
        if (order == null) {
            return ResultData.failDate(ResultCode.ORDER_NOT_EXIST.getCode(), ResultCode.ORDER_NOT_EXIST.getMsg(), "");
        } else {
            String[] split = order.getVisitTime().split(",");
            String datePoor = DateUtil.getDatePoor(DateUtil.getDateByYmdHms(split[1]), new Date());
            if (order.getStatus() == 0) {//未完成订单
                map.put("status", 0);
                map.put("statusString", datePoor);
            } else if (order.getStatus() == 1) {//已接单
                map.put("status", 1);
                map.put("statusString", datePoor);

            } else if (order.getStatus() == 2) {//订单失败
                map.put("status", 2);
                map.put("statusString", "");
            } else if (order.getStatus() == 3) {
                map.put("status", 2);
                map.put("statusString", "");
            }
        }
        if (!StringUtil.isEmptys(order.getBpId())) {
            BankPerson bankPerson = bankPersonMapper.selectByPrimaryKey(order.getBpId());
            map.put("bankPersonName", bankPerson.getUsername());
            map.put("bankPersonMobile", bankPerson.getMobile());
            map.put("bankPersonWorkNum", bankPerson.getWorkNumber());
            map.put("bankPersonAvatar", bankPerson.getWorkImg());
        }
        map.put("bankName", order.getBankName());
        map.put("address", order.getAddress());
        map.put("vistiTime", order.getVisitTime());
        map.put("orderId", order.getId());
        map.put("createTime", order.getCreateTime());
        map.put("closeTime", order.getCloseTime());
        map.put("getOrderTime", order.getGetOrderTime());
        return ResultData.successDate(map);
    }

    /**
     * 取消订单
     *
     * @param id
     * @return
     */
    @Override
    public ResultData updOrderStatus(Integer id,Integer status) {
        Order order = orderMapper.selectByPrimaryKey(id);
        if (order == null) {
            return ResultData.failDate(ResultCode.ORDER_NOT_EXIST.getCode(), ResultCode.ORDER_NOT_EXIST.getMsg(), "");
        }
        order.setStatus(status);
        int i = orderMapper.updateByPrimaryKeySelective(order);
        return ResultData.successDate(i);
    }

}
